package org.example.jcclubsubjectapplicationcontroller.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author 啊昌
 * @date 2025/4/13 15:19
 * GlobalConfig mvc的全局处理
 */
@Configuration
public class GlobalConfig  extends WebMvcConfigurationSupport {
	@Override
	protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		super.configureMessageConverters(converters);
//		添加自定义的 Jackson JSON 转换器
		converters.add(mappingJackson2HttpMessageConverter());
	}

	/**
	 * 自定义mappingJackson2HttpMessageConverter
	 * 目前实现：空值忽略，空字段可返回
	 * 创建并配置自定义的 Jackson JSON 消息转换器
	 */
	private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		return new MappingJackson2HttpMessageConverter(objectMapper);
	}
}
